﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Caching;

namespace D3DSS.Utility
{
    /// <summary>
    /// 缓存管理器
    /// </summary>
    public class CacheManager
    {
        /// <summary>
        /// 获取
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T Get<T>(CacheRegion region, object key)
        {
            return (T)MemoryCache.Default.Get(region.ToString() + "_" + key.ToString());
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="action"></param>
        public static void Set(CacheRegion region, object key, object value, int expireminute, bool isSliding = true)
        {
            CacheItem cacheitem = new CacheItem(region.ToString() + "_" + key.ToString(), value);
            CacheItemPolicy cacheItemPolicy = new CacheItemPolicy();
            if (isSliding)
            {
                cacheItemPolicy.SlidingExpiration = TimeSpan.FromMinutes(expireminute);
            }
            else
            {
                cacheItemPolicy.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(expireminute);
            }
            MemoryCache.Default.Add(cacheitem, cacheItemPolicy);
        }
    }
}
